package com.wlt.security;

import com.alibaba.fastjson2.JSON;
import com.wlt.result.BaseResult;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import java.io.IOException;

/**
 * 权限不足处理器
 */
public class MyAccessDeniedHandler implements AccessDeniedHandler
{
    @Override
    public void handle (
        HttpServletRequest request,
        HttpServletResponse response,
        AccessDeniedException accessDeniedException) throws IOException, ServletException
    {
        BaseResult baseResult = new BaseResult(403, "权限不足", null);
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(baseResult));
    }
}
